package top.lshaci.learning.framework.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * IntegerOptionalConstraintValidator
 *
 * @author lshaci
 * @since 1.0.0
 */
public class IntOptionalConstraintValidator implements ConstraintValidator<IntOptional, Integer> {

    private Set<Integer> allowValue = new HashSet<>();

    @Override
    public void initialize(IntOptional constraintAnnotation) {
        int[] value = constraintAnnotation.value();
        for (int i : value) {
            allowValue.add(i);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return allowValue.contains(value);
    }
}
